package com.saa.menu.service;



import com.saa.application.pojo.PageBean;
import com.saa.common.vo.OmsResultVo;
import com.saa.menu.dao.SaaApplicationMenuDao;
import com.saa.menu.dao.SaaMenuDao;
import com.saa.menu.po.SaaApplicationMenu;
import com.saa.menu.po.SaaMenu;
import com.saa.menu.vo.SaaApplicationMenuVo;
import com.saa.menu.vo.SaaMenuVo;
import ins.framework.utils.Beans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SaaMenuService {
    public static final Logger logger = LoggerFactory.getLogger(SaaMenuService.class);

    @Autowired
    private SaaMenuDao saaMenuDao;

    @Autowired
    private SaaApplicationMenuDao saaApplicationMenuDao;

    /****根据应用系统获取菜单***/
    public OmsResultVo queryMenu(SaaApplicationMenuVo saaApplicationMenuVo) throws Exception {
        List<SaaMenuVo> saaMenuVos_all = new ArrayList<SaaMenuVo>();
        List<SaaMenuVo> saaMenuVos_one = new ArrayList<SaaMenuVo>();
        Map<String, Object> params = new HashMap<>();
        params.put("applicationCode",saaApplicationMenuVo.getApplicationCode());
        List<SaaApplicationMenu> saaApplicationMenus = saaApplicationMenuDao.selectByCondition(params);
        for(SaaApplicationMenu saaApplicationMenu :saaApplicationMenus){
            SaaMenu saaMenu = saaMenuDao.selectByPrimaryKey(saaApplicationMenu.getMenuId());
            SaaMenuVo saaMenuVo_all = new SaaMenuVo();
            SaaMenuVo saaMenuVo_one = new SaaMenuVo();
            Beans.copy().from(saaMenu).to(saaMenuVo_all);
            saaMenuVos_all.add(saaMenuVo_all);
            if(saaMenu.getPid()==null){
                Beans.copy().from(saaMenu).to(saaMenuVo_one);
                saaMenuVos_one.add(saaMenuVo_one);
            }
        }
        for(SaaMenuVo saaMenuVo:saaMenuVos_one){
            saaMenuVo.setSaaMenuVoList(this.getSubMenu(saaMenuVo,saaMenuVos_all));
        }
        return OmsResultVo.builder(200, "查询成功", saaMenuVos_one);
    }

    /****菜单递归****/
    public List<SaaMenuVo> getSubMenu(SaaMenuVo saaMenuVo,List<SaaMenuVo> saaMenuVos_all){
        List<SaaMenuVo> saaMenuVos = new ArrayList<SaaMenuVo>();
        for(SaaMenuVo saaMenuVo_all:saaMenuVos_all){
            if(saaMenuVo.getMenuId().equals(saaMenuVo_all.getPid())){
                SaaMenuVo menuVo = saaMenuVo_all;
                if(saaMenuVo_all.getSubCount()>0){
                    List<SaaMenuVo> saaMenuVos_sub = this.getSubMenu(saaMenuVo_all,saaMenuVos_all);
                    menuVo.setSaaMenuVoList(saaMenuVos_sub);
                }
                saaMenuVos.add(menuVo);
            }
        }
        return saaMenuVos;
    }

    /****根据菜单ID获取菜单详情***/

    /****新增菜单***/

    /****停用菜单***/

    /****修改菜单***/
}
